package com.bac.bacplatform;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.bac.utils.Config;
import com.bac.utils.Util;

public class EditLoginPwdActivity extends BaseActivity implements OnClickListener{
	private String cur_password;
	private String new_password;
	private String confirm_password;
	private EditText cur_pwdEditText,new_pwdEditText,confirm_pwdEditText;
	private ImageButton clearCurPwdButton,clearNewPwdButton,clearConfirmPwdButton;
	private Button mConfirmButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginpassword_edit);
		findViewById(R.id.title_left).setOnClickListener(this);

	}

	@Override
	public void init() {
		cur_pwdEditText =(EditText) findViewById(R.id.cur_loginpwd_text);
		new_pwdEditText = (EditText) findViewById(R.id.new_loginpwd_text);
		confirm_pwdEditText = (EditText) findViewById(R.id.confirm_loginpwd_text);
		
		mConfirmButton = (Button) findViewById(R.id.loginpassword_edit_btn);
		mConfirmButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ChangePwd();
			}
		});
		
		clearCurPwdButton = (ImageButton) findViewById(R.id.btn_clear_cur_password);
		clearNewPwdButton = (ImageButton) findViewById(R.id.btn_clear_new_password);
		clearConfirmPwdButton = (ImageButton) findViewById(R.id.btn_clear_confirm_password);
		
		clearCurPwdButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				cur_pwdEditText.setText("");
			}
		});
		clearNewPwdButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new_pwdEditText.setText("");
			}
		});
		clearConfirmPwdButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				confirm_pwdEditText.setText("");
			}
		});
		
		cur_pwdEditText.addTextChangedListener(textWatcher);
		cur_pwdEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (cur_pwdEditText.getText().toString() != null
							&& !("").equals(cur_pwdEditText.getText().toString())) {
						clearCurPwdButton.setVisibility(View.VISIBLE);
						clearNewPwdButton.setVisibility(View.INVISIBLE);
						clearConfirmPwdButton.setVisibility(View.INVISIBLE);
					}
				}else {
					clearCurPwdButton.setVisibility(View.INVISIBLE);
				}
			}
		});
		new_pwdEditText.addTextChangedListener(textWatcher);
		new_pwdEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (new_pwdEditText.getText().toString() != null
							&& !("").equals(new_pwdEditText.getText().toString())) {
						clearNewPwdButton.setVisibility(View.VISIBLE);
						clearCurPwdButton.setVisibility(View.INVISIBLE);
						clearConfirmPwdButton.setVisibility(View.INVISIBLE);
					}
				}else {
					clearNewPwdButton.setVisibility(View.INVISIBLE);
				}
			}
		});
		confirm_pwdEditText.addTextChangedListener(textWatcher);
		confirm_pwdEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (confirm_pwdEditText.getText().toString() != null
							&& !("").equals(confirm_pwdEditText.getText().toString())) {
						clearConfirmPwdButton.setVisibility(View.VISIBLE);
						clearCurPwdButton.setVisibility(View.INVISIBLE);
						clearNewPwdButton.setVisibility(View.INVISIBLE);
					}
				}else {
					clearConfirmPwdButton.setVisibility(View.INVISIBLE);
				}
			}
		});

	}
	TextWatcher textWatcher = new TextWatcher() {
		
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void afterTextChanged(Editable s) {
			if (cur_pwdEditText.hasFocus() 
					&& cur_pwdEditText.getText().toString() != null 
					&& !("").equals(cur_pwdEditText.getText().toString())) {
				clearCurPwdButton.setVisibility(View.VISIBLE);
			}else {
				clearCurPwdButton.setVisibility(View.INVISIBLE);
			}
			
			if (new_pwdEditText.hasFocus()
					&& new_pwdEditText.getText().toString() != null
					&& !("").equals(new_pwdEditText.getText().toString())) {
				clearNewPwdButton.setVisibility(View.VISIBLE);
			}else {
				clearNewPwdButton.setVisibility(View.INVISIBLE);
			}
			
			if (confirm_pwdEditText.hasFocus()
					&& confirm_pwdEditText.getText().toString() != null
					&& !("").equals(confirm_pwdEditText.getText().toString())) {
				clearConfirmPwdButton.setVisibility(View.VISIBLE);
			}else {
				clearConfirmPwdButton.setVisibility(View.INVISIBLE);
			}
			
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left:
			finishActivityOut();
			break;
		default:
			break;
		}
	}

	private void ChangePwd() {
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i("TAG----success", response.toString());
				
				showToast("密码修改成功!");
				Intent intent = new Intent(EditLoginPwdActivity.this,LoginActivity.class);
				startActivity(intent);
			}
		};
		
		cur_password = cur_pwdEditText.getText().toString().trim();
		new_password = new_pwdEditText.getText().toString().trim();
		confirm_password = confirm_pwdEditText.getText().toString().trim();
		//数据验证
		if (cur_password.length() <= 0) {
			showToast(R.string.password_none);
			return;
		}
		if (new_password.length() <= 0 || confirm_password.length() <= 0) {
			showToast(R.string.input_newpassword);
			return;
		}
		if (!(new_password.equals(confirm_password))) {
			showToast(R.string.different_pwd);
			return;
		}
		if (new_password.equals(cur_password)) {
			showToast(R.string.same_pwd); 
			return;
		}
		
		showLoadingProgress();
		String path = Config.URL_CHANGE_LOGIN_PASSWORD;
		Map<String, String> map = new HashMap<String, String>();
		map.put("oldPassword",cur_password);
		map.put("password", new_password);
		Util.httpStringPost(path, sL, this, map);
		
	}
	
	public void getCurrentuser() {
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i("TAG----success", response.toString());
			}
		};

		String path = Config.URL_CURRENT_USER;
		Util.httpRequestToGet(path, sL, this);
	}

}
